﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using KPIS.GERP.GHRMS.BLL;
using Telerik.Web.UI;
using System.Data;
using KPIS.GERP.GHRMS.APP.Classes;
using System.Text;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class HRDEvaluationRoundCriteria : System.Web.UI.Page
    {
        private HRDEvaluationBLL cmd = new HRDEvaluationBLL();
        private SsMng ssMng = new SsMng();
        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string strYear = Convert.ToDateTime(DateTime.Now.ToString()).ToString("yyyy", new CultureInfo("th-TH"));
                txtRadYear.Text = strYear;
                LoadComboRoundYear(txtRadYear.Value.ToString());
            }
        }

        protected void txtRadYear_TextChanged(object sender, EventArgs e)
        {
            LoadComboRoundYear(txtRadYear.Value.ToString());
        }

        protected void LoadComboRoundYear(string strRoundYear)
        {
            cboRadRound.DataSource = cmd.LoadRound(strRoundYear);
            cboRadRound.DataBind();
        }

        protected void btnRadCheck_Click(object sender, EventArgs e)
        {
            DataTable dtPosSeq = cmd.getPosSeq(Convert.ToInt32(ssMng.EmpSeq));
            int intPosSeq = Convert.ToInt32(dtPosSeq.Rows[0]["pos_seq"]);

            var culture = new CultureInfo("en-US");

            int intYear = (int)txtRadYear.Value;

            if (cboRadRound.SelectedValue != "")
            {
                int intRoundSeq = Convert.ToInt32(cboRadRound.SelectedValue);
                DataTable dt = cmd.CheckEvaluation(Convert.ToInt32(ssMng.EmpSeq), intRoundSeq);

                if (dt.Rows.Count > 0)
                {
                    string strUrl = string.Format("HRDEvaluation.aspx?EvalSeq=" + intRoundSeq);
                    ClientScript.RegisterStartupScript(this.Page.GetType(), "", "window.open('" + strUrl + "');", true);
                }
                else
                {
                    DataTable dtCheckRound = cmd.CheckRoundPermission(Convert.ToInt32(cboRadRound.SelectedValue));

                    DateTime PermissionStartDate = Convert.ToDateTime(dtCheckRound.Rows[0]["permission_start_date"],culture);
                    DateTime PermissionEndDate = Convert.ToDateTime(dtCheckRound.Rows[0]["permission_end_date"], culture);
                    DateTime RoundStartDate = Convert.ToDateTime(dtCheckRound.Rows[0]["round_start_date"], culture);
                    DateTime RoundEndDate = Convert.ToDateTime(dtCheckRound.Rows[0]["round_end_date"], culture);

                    string strPermissionStartDate = PermissionStartDate.ToShortDateString();
                    string strPermissionEndDate = PermissionEndDate.ToShortDateString();
                    string strRoundStartDate = RoundStartDate.ToShortDateString();
                    string strRoundEndDate = RoundEndDate.ToShortDateString();
                    string dateNow = DateTime.Now.ToShortDateString().ToString(culture);

                    if ((DateTime.Parse(dateNow) >= DateTime.Parse(strPermissionStartDate)) && (DateTime.Parse(dateNow) <= DateTime.Parse(strPermissionEndDate)))
                    {
                        DataTable dtCheckData = cmd.LoadData(Convert.ToInt32(ssMng.EmpSeq));
                        bool boolIsValid = Convert.ToBoolean(dtCheckData.Rows[0]["isvalid"]);
                        if (boolIsValid)
                        {
                            string strUrl = string.Format("HRDEvaluation.aspx?EvalSeq=" + intRoundSeq);
                            ClientScript.RegisterStartupScript(this.Page.GetType(), "", "window.open('" + strUrl + "');", true);
                        }
                        else
                        {
                            main.MessageAlert(Page, "ข้อมูลไม่ครบ กรุณาติดต่อHR");
                        }
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        //sb.AppendFormat("วันที่ปัจจุบันไม่ได้อยู่ในช่วงที่อนุญาตให้ประเมินของรอบนี้ \n รอบการประเมินที่ : {1}/{0}", intYear.ToString(), cboRadRound.SelectedValue.ToString());
                        string strStartDate = PermissionStartDate.ToString("dd/MMM/yyyy", new CultureInfo("th-TH"));
                        string strEndDate = PermissionEndDate.ToString("dd/MMM/yyyy", new CultureInfo("th-TH"));
                        sb.AppendFormat("วันที่อนุญาตให้ประเมิน : {0} - {1} ", strStartDate, strEndDate);

                        main.MessageAlert(Page, sb.ToString());
                    }
                }
                
            }
            else
            {
                main.MessageAlert(Page, "ยังไม่มีรอบการประเมิน");
            }


            
            

            
        }
    }
}